package com.example.goodpet.home.homeRecommend.adapter.viewholder;

import android.content.Context;
import android.view.View;
import android.widget.Toast;


import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.goodpet.R;
import com.example.goodpet.bean.BannerInfoDTO;
import com.example.goodpet.bean.MainChannel;
import com.example.goodpet.home.homeRecommend.bean.ResultBeanData;
import com.example.goodpet.utils.Constants;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.transformer.ScaleInTransformer;


import java.util.ArrayList;
import java.util.List;

public class BannerViewHolder extends RecyclerView.ViewHolder {
    private Context mContext;
    private Banner banner;
    public BannerViewHolder(Context mContext, View itemView) {
        super(itemView);
        this.mContext=mContext;
        banner=itemView.findViewById(R.id.banner);
    }

    public void setData(List<BannerInfoDTO> bannerInfo) {
        List<String> imagesUrl = new ArrayList<>();
        for (int i = 0; i < bannerInfo.size(); i++) {
            String imageUrl = bannerInfo.get(i).getImage();
            imagesUrl.add(imageUrl);
        }
        //设置banner的数据
        banner.setAdapter(new BannerImageAdapter(imagesUrl) {
            @Override
            public void onBindView(Object holder, Object data, int position, int size) {
//                Log.e("TAG", "onBindView: "+Constants.BASE_URL_IMAGE+data );
                Glide.with(mContext)
                        .load(Constants.BASE_URL_IMAGE+data)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                        .into(((BannerImageHolder) holder).imageView);
            }
        })
                .setPageTransformer(new ScaleInTransformer())
                .addBannerLifecycleObserver((LifecycleOwner) mContext)//添加生命周期观察者
                .setIndicator(new CircleIndicator(mContext));
        //横幅不设点击事件 后期如需点击事件可重新设置监听
//        banner.setOnBannerListener(new OnBannerListener() {
//            @Override
//            public void OnBannerClick(Object data, int position) {
//                //设置点击跳转的页面
//                Toast.makeText(mContext, "第"+position+"张图片"+data+"被点击了", Toast.LENGTH_SHORT).show();
//            }
//        });
    }

}
